* Purpose: To generate randomization balance tables for the info_study
* Author: Sadish
* Last Modified: 2021-04-22
* Notes:

version 13

* Inputs **************************************************************
  local data "${is_root}/6_Processed_Data/is_4_4_6_publicData.dta"
  local balance "${is_root}/7_Output/is_4_4_2_randomizationTable.tex"
  local attrition "${is_root}/7_Output/is_4_4_2_attritionTable.tex"
  local uid "empcode" // variables to uniquely identify individuals
  local balVars "female age edu_hi quit smartphone hostel numeracy memory knowledge_bl distress_bl"
***********************************************************************


clear all
set more off
set scheme lean1 // plottig
tempfile box

* Get Clean Data ******************************************************
use "`data'", clear
save `box', replace
***********************************************************************


* Randomization Balance Check *****************************************
cap: file close bal
file open bal using "`balance'", write replace
file write bal ///
   "\begin{tabular}{a{4cm} *{3}{b{1cm}} *{3}{b{1.6cm}}} \toprule % The *{#} repeats the following argument for # columns. a{} is ragged right column, b{} is centered." _n
file write bal " Variable & TM & VR & PC & TM - VR & VR - PC & TM - PC \\ \midrule" _n

foreach v in `balVars' {
  local la_`v': var lab `v'
  
  su `v' if TM==1
  local mean_TM = r(mean)
  
  su `v' if VR==1
  local mean_VR = r(mean)
  
  su `v' if PC==1
  local mean_PC = r(mean)
  
  ttest `v' if TM==1 | VR==1, by(VR)
  local se_TM_VR = "(" + string(r(se), "%12.2fc") + ")"
  local mean_TM_VR = `mean_TM' - `mean_VR'
  local mean_TM_VR = string(`mean_TM_VR', "%12.2fc")
  
  ttest `v' if VR==1 | PC==1, by(PC)
  local se_VR_PC = "(" + string(r(se), "%12.2fc") + ")"
  local mean_VR_PC = `mean_VR' - `mean_PC'
  local mean_VR_PC = string(`mean_VR_PC', "%12.2fc")
  
  ttest `v' if TM==1 | PC==1, by(PC)
  local se_TM_PC = "(" + string(r(se), "%12.2fc") + ")"
  local mean_TM_PC = `mean_TM' - `mean_PC'
  local mean_TM_PC = string(`mean_TM_PC', "%12.2fc")
  
  local mean_TM = string(`mean_TM', "%12.2fc")
  local mean_VR = string(`mean_VR', "%12.2fc")
  local mean_PC = string(`mean_PC', "%12.2fc")
  
  file write bal "`la_`v'' & `mean_TM' & `mean_VR' & `mean_PC' & `mean_TM_VR' & `mean_VR_PC' & `mean_TM_PC' \\ " _n
  file write bal " & & & & `se_TM_VR' & `se_VR_PC' & `se_TM_PC' \\ " _n
}

file write bal "\bottomrule" _n
file write bal "\end{tabular}" _n
file close bal
***********************************************************************

* Attrition Balance Check *********************************************
cap: file close att
file open att using "`attrition'", write replace
file write att ///
   "\begin{tabular}{a{4cm} *{3}{b{2.2cm}}} \toprule % The *{#} repeats the following argument for # columns. a{} is ragged right column, b{} is centered." _n
file write att " Variable & Non-Attrited & Attrited & Difference \\ \midrule" _n

foreach v in `balVars' TM VR PC {
  local la_`v': var lab `v'
  
  su `v' if att==0
  local mean = r(mean)
  
  su `v' if att==1
  local mean_att = r(mean)
  
  ttest `v', by(att)
  local se_att = "(" + string(r(se), "%12.2fc") + ")"
  local mean_diff = `mean' - `mean_att'
  local mean_diff = string(`mean_diff', "%12.2fc")
  
  local mean = string(`mean', "%12.2fc")
  local mean_att = string(`mean_att', "%12.2fc")
  
  file write att "`la_`v'' & `mean' & `mean_att' & `mean_diff' \\ " _n
  file write att " & & & `se_att' \\ " _n
}

file write att "\bottomrule" _n
file write att "\end{tabular}" _n
file close att
***********************************************************************
